// var videoUtil = require('../../utils/videoUtil.js')

const app = getApp()

Page({
  data: {
    faceUrl: "../resource/images/noneface.png",
    isMe: true,
    isFollow: false,

    videoSelClass: "video-info",
    isSelectedWork: "video-info-selected",
    isSelectedLike: "",
    isSelectedFollow: "",
    
    myVideoList: [],
    myVideoPage: 1,
    myVideoTotal: 1,

    likeVideoList: [],
    likeVideoPage: 1,
    likeVideoTotal: 1,

    followVideoList: [],
    followVideoPage: 1,
    followVideoTotal: 1,

    myWorkFalg: false,
    myLikesFalg: true,
    myFollowFalg: true

  },

  onLoad:function(params) {
    var me = this
    //换为本地缓存
    // var userInfo = app.userInfo
    var user = app.getGlobalUserInfo();
    var userId = user.id;

    var publisherId = params.publisherId;
    if (publisherId != null && publisherId != '' && publisherId != undefined) {
      userId = publisherId;
      me.setData({
        isMe: false,
        publisherId: publisherId,
        serverUrl: app.serverUrl
      })
    }
    me.setData({
      userId: userId
    })

    var serverUrl = app.serverUrl;
    wx.showLoading({
      title: '少女祈祷中',
    }),
    wx.request({
      url: serverUrl + '/user/findById.do?usersId=' + userId + "&fanId=" + user.id ,
      method: 'POST',
      header:{
          'content-type': 'application/json',
          'headerUserId': user.id,
          'headerUserToken': user.userToken
      },
      success: function(res){
          wx.hideLoading();
          if(res.data.status == 200){

              var userInfo = res.data.data;
              var faceUrl = "../resource/images/noneface.png";
              //对传入的userinfo的头像地址判空。若空则使用默认头像。
              
              if(userInfo.faceImage != null && userInfo.faceImage != "" &&
              userInfo.faceImage != undefined){
                faceUrl = serverUrl + userInfo.faceImage;
              }

              me.setData({
                faceUrl: faceUrl,
                fansCounts: userInfo.fansCounts,
                followCounts: userInfo.followCounts,
                receiveLikeCounts: userInfo.receiveLikeCounts,
                nickname: userInfo.nickname,
                isFollow: userInfo.follow
              });

              console.log(userInfo);
          }else if(res.data.status == 502){
            wx.showToast({
              title: 'res.data.msg',
              duration: 3000,
              icon: 'none',
              success: function(){
                wx.redirectTo({
                  url: '../userLogin/login',
                })
              }
            })
          }
          else{
            wx.showToast({
                title: '查询用户信息失败',
                icon: "none",
                duration: 3000
            })
          }
      }
    })
    // me.getMyVideoList(1);
  },

  logout:function(e){
    //换为本地缓存
    // var user = app.userInfo;
    var user = app.getGlobalUserInfo();
    
    var serverUrl = app.serverUrl;
    wx.showToast({
      title: '少女祈祷中...',
    })
    //调用后端
    wx.request({
      url: serverUrl + '/logout.do?userId=' + user.id,
      method: 'POST',
      header:{
          'content-type': 'application/json'
      },
      success: function(res){
          wx.hideLoading();
          console.log(res.data);
          if(res.data.status == 200){
              wx.showToast({
                title: res.data.msg,
                icon: "success",
                duration: 3000
              })
              //换为本地缓存
              // app.userInfo = res.data.data;
              //注销后，清空本地缓存
              wx.removeStorageSync('userInfo');

              //TODO:页面跳转
              wx.redirectTo({
                url: '../userLogin/login',
              })
          }
          else{
              wx.showToast({
                  title: res.data.msg,
                  icon: "none",
                  duration: 3000
                })
          }
      }
    }
  )

  },

  changeFace:function(e){
    var me = this;   //统一作用域
    //将原来的全局变量换位本地缓存
    var userInfo = app.getGlobalUserInfo();
    wx.chooseImage({
      count: 1,
      sizeType:['compressed'],
      sourceType:['album','camera'],
      success:function(res){
        var tempFilePaths = res.tempFilePaths;
        console.log(tempFilePaths);

        var serverUrl = app.serverUrl;
        wx.showLoading({
          title: '上传中',
        })
        wx.uploadFile({
          url: serverUrl + '/user/uploadFace.do?userId=' + userInfo.id,
          filePath: tempFilePaths[0],
          name: 'file',
          success:function(res){
            wx.hideLoading()
            var resultData = JSON.parse(res.data)   //格式化。官方规定返回图片地址为一个json数据。
            console.log(resultData)
            if(resultData.status == 200){
              wx.showToast({
                title: '头像上传成功!',
                icon: 'success',
                duration: 3000
              })
              //注意一定要加上 serverUrl 作为前缀
              var imgUrl = serverUrl + resultData.data;
              me.setData({
                faceUrl: imgUrl
              })
            }
            else{
              wx.showToast({
                title: '头像上传失败!',
                icon:'none',
                duration: 3000
              });
            }
          }
        })
      }
    })

  },

  uploadVideo:function(e){
    var me = this;

    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      success:function(res){
        console.log(res)

        var duration = res.duration;
        var tmpHeight = res.height;
        var tmpWidth = res.width;
        var tmpVideoUrl = res.tempFilePath;
        var tmpCoverUrl = res.thumbTempFilePath;

        if(duration > 16 ){
          wx.showToast({
            title: '上传视频长度不能超过15秒',
            icon: 'none',
            duration: 3000
          })
        }
        else if(duration < 5){
          wx.showToast({
            title: '上传视频长度不能小于5秒',
            icon: 'none',
            duration: 3000
          })
        }
        else{
          // 打开选择bgm页面
          wx.navigateTo({
            url: '../chooseBgm/chooseBgm?duration='+ duration +
            '&tmpHeight=' + tmpHeight +
            '&tmpWidth=' + tmpWidth +
            '&tmpVideoUrl=' + tmpVideoUrl +
            '&tmpCoverUrl=' + tmpCoverUrl,
            
          })
        }
      }
    })
  },

  followMe:function(e){
    var me = this;
    var user = app.getGlobalUserInfo();
    var userId = user.id;
    var publisherId = me.data.publisherId;

    var followType = e.currentTarget.dataset.followtype;

    //1: 关注， 0:取消关注
    var url = '';
    if(followType=='1'){
      //关注接口
      url = '/user/beFans?userId=' + publisherId + '&fanId=' + userId;
    }else{
      //取关接口
      url = '/user/dontBeFans?userId=' + publisherId + '&fanId=' + userId;
    }
    wx.showLoading();
    wx.request({
      url: app.serverUrl + url,
      method: 'POST',
      header:{
        'content-type': 'application/json',
        'headerUserId': user.id,
        'headerUserToken': user.userToken
      },
      success:function(){
        wx.hideLoading();
        if(followType == '1'){
          me.setData({
            isFollow: true,
            fansCounts: ++me.data.fansCounts
          })
        }else{
          me.setData({
            isFollow: false,
            fansCounts: --me.data.fansCounts
          })
        }
      }
    })
  },
})
